package ua.org.nuos.sdms.clientgui.client.view;

import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.*;
import com.vaadin.ui.themes.ChameleonTheme;
import ua.org.nuos.sdms.clientgui.client.components.factory.ButtonFactory;
import ua.org.nuos.sdms.clientgui.client.components.factory.LayoutFactory;
import ua.org.nuos.sdms.clientgui.client.controller.GlobalBoard;
import ua.org.nuos.sdms.clientgui.client.events.*;
import ua.org.nuos.sdms.clientgui.client.listeners.EntityChangedListener;
import ua.org.nuos.sdms.clientgui.client.listeners.UserChangedListener;
import ua.org.nuos.sdms.clientgui.server.AppProxy;
import ua.org.nuos.sdms.middle.entity.Group;
import ua.org.nuos.sdms.middle.entity.User;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 19.02.12
 * Time: 18:49
 * To change this template use File | Settings | File Templates.
 */
public class Header extends HorizontalLayout implements UserChangedListener, EntityChangedListener<Group> {

    private static final String HEIGHT = "30";
    private static final String EMPTY_WIDTH = "30";
    private static final String LOGIN_LOGOUT_WIDTH = "40";

    private static final String LOGIN = "Войти";
    private static final String LOGOUT = "Выйти";

    private HorizontalLayout tray;
    private HorizontalLayout groupLayout;
    private Label userNameLabel;
    private Button loginLogoutButton;
    private Label groupNameLabel;
    private MenuBar config;

    public Header() {
        init();
    }

    private void init() {
        setHeight(HEIGHT);
        AppProxy.getInstance().getController(GlobalBoard.ID).addListener(this);
        AppProxy.getInstance().getModelLocator().getGroupContainer().addListener(this);

        refreshUserContent();
        refreshGroupContent(AppProxy.getInstance().getModelLocator().getGroup());

        groupLayout = new HorizontalLayout();
        groupLayout.setSpacing(true);
        groupLayout.addComponent(groupNameLabel);
        groupLayout.addComponent(config);

        tray = new HorizontalLayout();
        tray.setSpacing(true);
        tray.addComponent(groupLayout);
        tray.addComponent(LayoutFactory.getHEmptyLayout(EMPTY_WIDTH));
        tray.addComponent(userNameLabel);
        tray.addComponent(loginLogoutButton);

        setWidth("100%");
        setStyleName("toolbar-background");
        setMargin(false);

        addComponent(tray);
        setComponentAlignment(tray, Alignment.MIDDLE_RIGHT);
    }

    private void refreshUserContent() {
        User user = AppProxy.getInstance().getServiceLocator().getAuthenticationBean().getCurrentUser();
        if (userNameLabel == null) {
            userNameLabel = new Label();
            userNameLabel.setStyleName(ChameleonTheme.LABEL_H4);
            userNameLabel.addStyleName("header_label");
        }
        if (loginLogoutButton == null) {
            loginLogoutButton = ButtonFactory.getButton("", getLoginLogoutClickListener());
            loginLogoutButton.setStyleName(ChameleonTheme.BUTTON_SMALL);
            loginLogoutButton.addStyleName("borderless");
            loginLogoutButton.setWidth(LOGIN_LOGOUT_WIDTH);
        }
        if (user == null) {
            userNameLabel.setValue("");
            loginLogoutButton.setCaption(LOGIN);
        } else {
            userNameLabel.setValue(user.getFullName());
            loginLogoutButton.setCaption(LOGOUT);
        }
    }

    private void refreshGroupContent(Group group) {
        if (groupNameLabel == null) {
            groupNameLabel = new Label();
            groupNameLabel.setStyleName(ChameleonTheme.LABEL_H4);
            groupNameLabel.addStyleName("header_label");
        }
        if (config == null) {
            config = new MenuBar();
            config.setStyleName("borderless");
            config.addStyleName("menubar_toolbar");

            MenuBar.MenuItem cg = config.addItem("", new ThemeResource("../sdms/icons/down_7.png"), null);
            cg.addItem("Изменить группу", getChangeGroupCommand());
        }

        if (group == null) {
            groupNameLabel.setValue("");
            config.setVisible(false);
        } else {
            groupNameLabel.setValue(group.getName());
            config.setVisible(true);
        }
    }

    @Override
    public void userChanged(UserChangedEvent event) {
        refreshUserContent();
    }

    @Override
    public void changed(EntityChangedEvent<Group> event) {
        refreshGroupContent(event.getEntityContainer().getEntity());
    }

    private Button.ClickListener getLoginLogoutClickListener() {
        return new Button.ClickListener() {

            @Override
            public void buttonClick(Button.ClickEvent event) {
                User user = AppProxy.getInstance().getServiceLocator().getAuthenticationBean().getCurrentUser();
                if (user == null) {
                    AppProxy.getInstance().getController(GlobalBoard.ID).fire(new ShowLoginFormEvent());
                } else {
                    AppProxy.getInstance().getController(GlobalBoard.ID).fire(new LogoutEvent());
                }
            }
        };
    }

    private MenuBar.Command getChangeGroupCommand() {
        return new MenuBar.Command() {
            @Override
            public void menuSelected(MenuBar.MenuItem selectedItem) {
                AppProxy.getInstance().getController(GlobalBoard.ID).fire(new ShowSelectGroupEvent());
            }
        };
    }
}
